
/*

	Compute q values
	
	Turns the randomization inference p-values into q-values
	
*/

*make sure the date matches the date the results were run, or this will not work.
local date="$S_DATE"

local tests EGRA EGWA OE

foreach test of local tests{

	local types ANCOVA Raw
	local samples all No_VHP_Cell

	foreach type of local types{

		foreach sample of local samples{

			local filename `test' `version' Table `date'`type'${year}`sample'

			quietly{
				*for testing
				import excel using "${dir}/`filename'.xlsx", clear firstrow cellrange(A3)
				
				*get rid of indices for this process
				capture drop *Index*
				
				keep if inlist(A,"R.I. p-value")
				gen pval_type = "FC" if _n==1, after(A)
				replace pval_type = "RC" if _n==2
				replace pval_type = "Diff" if _n==3
				drop A
				
				unab a:_all
				local b pval_type
				local vars: list a - b
				
				di "`vars'"
				
				local nvars: word count `vars'
				
				*get rid of brackets
				forvalues i=1/`nvars'{
					local var : word `i' of `vars'
					replace `var' = subinstr(subinstr(`var',"]","",.),"[","",.)
					destring `var', replace
					
					rename `var' pval`i'
				}
				
				gen id = _n
				order id, before(pval_type)
				
				reshape long pval, i(id) j(j)
				
				*compute q-values
					*must use "simes" option to do the basic Benjamini and Yekutieli method
					*weird syntax in this command
				qqvalue pval, method(simes) qvalue(qvalFCRC), if inlist(pval_type,"FC","RC")
				qqvalue pval, method(simes) qvalue(qvalDiff), if inlist(pval_type,"Diff")
				
				gen qval = .
				replace qval = qvalFCRC if inlist(pval_type,"FC","RC")
				replace qval = qvalDiff if inlist(pval_type,"Diff")
				
			
				
				replace qval =round(qval,0.001)
				tostring qval, replace force format(%04.3f)
				replace qval = "{"+qval+"}"
				
				keep pval_type id j qval
				reshape wide qval, j(j) i(id)
				
				order id pval_type
				
				export excel "${dir}/`filename' qvals.xlsx", replace
				
			}
			*end block to do quietly
			
		}
		*end loop over samples
		
	}
	*end loop over types
		
}
*end loop over tests


*set trace on
local classobs_list Classobs_clpsd_Pupil_all_wgtd_raw ///
					Classobs_clpsd_Teacher_all_wgtd_raw ///
					Classobs_clpsd_Pupil_pct_paw15_minutes_on_writing_wgtd_raw ///
					Classobs_clpsd_Pupil_pct_par15_minutes_on_reading_wgtd_raw 
	

foreach classobs of local classobs_list{


	local filename `classobs'

	*quietly{
		*for testing
		import excel using "${dir}/`filename'.xlsx", clear firstrow cellrange(A3)
		
		
		keep if inlist(A,"R.I. p-value")
		gen pval_type = "FC" if _n==1, after(A)
		replace pval_type = "RC" if _n==2
		replace pval_type = "Diff" if _n==3
		drop A
		dropmiss, force
		
		unab a:_all
		local b pval_type
		local vars: list a - b
		
		di "`vars'"
		
		local nvars: word count `vars'
		
		*get rid of brackets
		forvalues i=1/`nvars'{
			local var : word `i' of `vars'
			replace `var' = subinstr(subinstr(`var',"]","",.),"[","",.)
			destring `var', replace
			
			rename `var' pval`i'
		}
		
		gen id = _n
		order id, before(pval_type)
		
		reshape long pval, i(id) j(j)
		
		*compute q-values
			*must use "simes" option to do the basic Benjamini and Yekutieli method
			*weird syntax in this command
		qqvalue pval, method(simes) qvalue(qvalFCRC), if inlist(pval_type,"FC","RC")
		qqvalue pval, method(simes) qvalue(qvalDiff), if inlist(pval_type,"Diff")
		
		gen qval = .
		replace qval = qvalFCRC if inlist(pval_type,"FC","RC")
		replace qval = qvalDiff if inlist(pval_type,"Diff")
		

		
		replace qval =round(qval,0.001)
		tostring qval, replace force format(%04.3f)
		replace qval = "{"+qval+"}"
		
		keep pval_type id j qval
		reshape wide qval, j(j) i(id)
		
		order id pval_type
		
		export excel "${dir}/`filename' qvals.xlsx", replace

	*}
	*end block to do quietly

}
*end loop over classobs
